package com.tanhua.dubbo.api.mongo.impl;

import com.tanhua.dubbo.api.mongo.UserLikeApi;
import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@DubboService
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(UserLike userLike) {
        mongoTemplate.save(userLike);
    }

    @Override
    public boolean isEachLike(Long userId, Long likeUserId) {
        Query query1 = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId).and("isLike").is(true));
        Query query2 = new Query(Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId).and("isLike").is(true));
        boolean ex1 = mongoTemplate.exists(query1, UserLike.class);
        boolean ex2 = mongoTemplate.exists(query2, UserLike.class);
        if(ex1&&ex2){
            return true;
        }
        return false;
    }

    @Override
    public void deleteEachLike(Long userId, Long likeUserId) {
        Query query1 = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId).and("isLike").is(true));
        Query query2 = new Query(Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId).and("isLike").is(true));
        mongoTemplate.remove(query1,UserLike.class);
        mongoTemplate.remove(query2,UserLike.class);
    }
}
